from wx import *
from wx.lib.printout import PrintTable

# Importation des fichiers de class

import co
import addposte

ADD=wx.NewId()
EDIT=wx.NewId()
CLOSE=wx.NewId()
DISPLAY=wx.NewId()
DELETE=wx.NewId()
PRINT=wx.NewId()
DETAILS=wx.NewId()
ACTUALISER=wx.NewId()


class managepostes(wx.Dialog):
    def __init__(self,parent,id,title, **kwds):
        #begin wxGlade: manage_users.__init__
        self.connect=co.co()
        self.idu = 0
        self.title = "Gestion des services"
        self.parent=parent
        kwds["style"] = (wx.DEFAULT_DIALOG_STYLE|wx.MINIMIZE_BOX|
                         wx.TAB_TRAVERSAL)#wx.RESIZE_BORDER
        wx.Dialog.__init__(self, parent,id,title, **kwds)

        self.lctsup = wx.ListCtrl(self, DISPLAY, style=wx.LC_REPORT|wx.LC_SINGLE_SEL, size=(1,150))

    #------------------------COLONNES----------------------------

        self.lctsup.InsertColumn(0,'id du poste')
        self.lctsup.InsertColumn(1,'Nom du poste')
        
     #------------------------BOUTONS------------------------------

        self.butadd = wx.Button(self, ADD, "Ajouter")
        self.butedit = wx.Button(self, EDIT, "Editer")
        self.butrefresh = wx.Button(self, ACTUALISER, "Actuaiser")
        self.butdelete = wx.Button(self, DELETE, "Supprimer")
        self.butprint = wx.Button(self, PRINT, "Imprimer")
        self.butclose = wx.Button(self, CLOSE, "Fermer")
        self.refresh()
        self.__do_layout()




    def __do_layout(self):
		#self.boxa = all
        #self.boxt = tableau
        #self.boxb = boutons
        self.boxa = wx.BoxSizer(wx.VERTICAL)
        self.boxt = wx.BoxSizer(wx.VERTICAL)
        self.boxb = wx.BoxSizer(wx.HORIZONTAL)
        self.boxt.Add(self.lctsup, 1, wx.EXPAND)
        self.boxb.Add(self.butadd, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxb.Add(self.butedit, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxb.Add(self.butrefresh, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxb.Add(self.butdelete, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxb.Add(self.butprint, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxb.Add(self.butclose, 0,wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL)
        self.boxa.Add(self.boxt,1, wx.EXPAND)
        self.boxa.Add(self.boxb,0, wx.EXPAND)
        self.SetAutoLayout(1)
        self.SetSizer(self.boxa)
        self.boxa.Fit(self)
        self.boxa.SetSizeHints(self)
        self.Layout()
        wx.EVT_BUTTON(self, ADD, self.add)
        wx.EVT_BUTTON(self, EDIT, self.edit)
        wx.EVT_BUTTON(self, ACTUALISER, self.brefresh)
        wx.EVT_BUTTON(self, DELETE, self.delete)
        wx.EVT_LIST_ITEM_SELECTED(self, DISPLAY, self.display)
        wx.EVT_BUTTON(self, CLOSE, self.close)
        wx.EVT_BUTTON(self, PRINT, self.onprint)

    def refresh(self):
        self.lctsup.DeleteAllItems()
        x = 0
        self.connect.fermerConnection()
        self.connect=co.co()
        self.posteList = self.connect.getposte() #external program
        for k in self.posteList:
            self.lctsup.InsertStringItem(x,str(k[0]))
            self.lctsup.SetStringItem(x,1,str(k[1]))
            x += 1
        self.lctsup.RefreshItems(0, x)
        print self.posteList

    def brefresh(self,event):
        self.lctsup.DeleteAllItems()
        x = 0
        self.connect.fermerConnection()
        self.connect=co.co()
        self.posteList = self.connect.getposte() #external program
        print self.posteList
        for k in self.posteList:
            self.lctsup.InsertStringItem(x,str(k[0]))
            self.lctsup.SetStringItem(x,1,str(k[1]))
            x += 1
        self.lctsup.RefreshItems(0, x)
		
    def add(self, event):
        aposte = addposte.addpostes(self, -1, "Ajouter un service",False, 0)
        val=aposte.ShowModal()
        self.refresh()
            
    def edit(self, event):
        if self.idu:
            aposte = addposte.addpostes(self, -1, "Editer un service",True, self.idu)
            val=aposte.ShowModal()
            if val:
                self.refresh()
                
    def delete(self, event):
        if self.idu:
            mg = "Are You Sure You want to delete ?"
            msg = wx.MessageDialog(self,mg,"Warning",wx.OK|wx.CANCEL)
            res = msg.ShowModal()
            msg.Destroy()
            if res == wx.ID_OK:
                tes = self.connect.deleteposte(self.idu) #external program
                if tes:
                    #display error message
                    error = wx.MessageDialog(self,"Delete Failed","Error",
                       wx.OK)
                    error.ShowModal()
                    error.Destroy()
                else:
                    print "refresh"
                    self.refresh()
                    
    def display(self,event):
        curitem = event.m_itemIndex
        litem = self.lctsup.GetItem(curitem,0).GetText()
        self.idu = litem
        


    def onprint(self,event):
        data = []
        data.append(["Id","Nom du poste"])
        for k in self.posteList:
            data.append([k[0],k[1]])
        prt = PrintTable(self.parent)
        prt.data = data[1:]
        prt.left_margin = 1.2
        prt.set_column = [ 2,2]
        prt.label = data[0]
        prt.top_margin = 2
        prt.SetLandscape()
        prt.SetHeader("Liste des postes", size = 50)
        prt.SetFooter("Page No", colour = wx.NamedColour('RED'), type ="Num")
        prt.SetRowSpacing(10,10)
        prt.Print()
        
        
    def close(self, event):
        self.Destroy()

class app(wx.App):
    def OnInit(self):
        frame=managepostes(None,-1,'')
        self.SetTopWindow(frame)
        frame.Show()
        return 1

if __name__ == "__main__":
    prog = app(0)
    prog.MainLoop()
